package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.entity.Borrow;
import com.example.demo.entity.BorrowHistory;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.BorrowMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.BorrowBookService;
import com.example.demo.utils.ResponseWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
public class BorrowController {
    @Autowired
    BorrowMapper borrowMapper;
    @Autowired
    BorrowBookService borrowBookService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BookMapper bookMapper;
    @RequestMapping("getBorrowHistory")
//    管理员-借阅历史
    public ResponseWrapper getBorrowHistory(int start){
        start=start*10;
        PageHelper.offsetPage(start,10);
        List<BorrowHistory> borrowHistories = borrowMapper.borrowHistory();
        PageInfo pageInfo = new PageInfo(borrowHistories,10);
        return ResponseWrapper.markSuccess(pageInfo);
    }

    @RequestMapping("searchBorrowHistory")
    public ResponseWrapper searchBorrowHistory(String bookname){
        System.out.println("searchBorrowHistory  "+bookname);
        String name="%"+bookname+"%";
        return ResponseWrapper.markSuccess(borrowMapper.searchBorrowHistory(name));
    }

    @RequestMapping("borrowBook")
    public boolean borrowBook(String username,int bookId){
        boolean result = false;
        System.out.println("username++"+username);
        System.out.println("bookId++"+bookId);
        if(borrowBookService.borrowBook(username,bookId)){
            result = true;
        }
        return result;
    }
}
